package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.entity.GoldPoints;
import com.lianxi.Courses.entity.User;
import com.lianxi.Courses.exception.PointErrorException;
import com.lianxi.Courses.exception.RequestParameterErrorException;
import com.lianxi.Courses.service.GoldPointsService;
import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 翔哥
 * 2025年10月13日
 */
@RestController
@RequestMapping("/goldPoints")
public class GoldPointsController {

    @Autowired
     private GoldPointsService goldPointsService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize, HttpSession session) {
        ModelAndView mav = new ModelAndView();
            User user = (User) session.getAttribute("user");

            PageInfo<GoldPoints> goldPointsPage = goldPointsService.findPage(String.valueOf(user.getId()), pageNo, pageSize);
            mav.addObject("goldPointsPage", goldPointsPage);
            mav.setViewName("backend/front_record");

        return mav;
    }

    @RequestMapping("/pointBuyFind")
    public ResponseResult pointBuyFind( String id, String point,HttpSession session) {
        ResponseResult result =new ResponseResult();
        try {
            goldPointsService.pointBuyFind(id, point,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (PointErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
